package com.xqx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author：xingquanxiang createTime：2019/10/4 23:02
 * description:
 */
@Controller
@RequestMapping(produces = "text/html;charset=utf-8")
public class TargetController {
    /**
     * http://localhost:8082/xqx_spring_interceptor/target
     * @param modelAndView
     * @return
     */
    @RequestMapping("/target")
    public ModelAndView target(ModelAndView modelAndView){
        System.out.println("目标资源执行。。。。");
        modelAndView.addObject("username", "xingquanxinag");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    // @RequestMapping(value = "/home", produces = "text/html;charset=utf-8")
    @RequestMapping(value = "/home")
    @ResponseBody
    public String home(HttpServletResponse response){
        return "这里是主页";
    }

    @RequestMapping("/doLogin")
    public String login(String username, String password, HttpSession session){
        String targetUri = (String)session.getAttribute("targetUri");
        System.out.println(targetUri);
        if ("xqx".equals(username)||"1998".equals(password)){
            session.setAttribute("username","xqx");
            session.setAttribute("password","1998");
            return "redirect:"+targetUri.substring(targetUri.lastIndexOf("/"));
        }else{
            return "redirect:/login.html";
        }

    }
}
